﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ElectronicShoppingMall.Service.Models.Clinet;
using ElectronicShoppingMall.Service.Data;

namespace ElectronicShoppingMall.Service.Services.Client
{
    public class OrderService: IDisposable
    {
        private ElectronicShoppingMallContext data;
        public OrderService()
        {
            data = new ElectronicShoppingMallContext();
        }
        /// <summary>
        ///确认下单
        /// </summary>
        /// <param name="confirmOrderModel"></param>
        public void ConfirmOrder(ConfirmOrderModel confirmOrderModel)
        {
            var client = data.Clients.SingleOrDefault(c => c.UserId == confirmOrderModel.UserId);
            var carts = data.ShoppingCarts.Where(s => s.ClientId == client.ClientId);
            foreach (var goodsInfo in confirmOrderModel.Goods)
            {
                var goods = data.GoodsInfoes.SingleOrDefault(g => g.GoodsId == goodsInfo.GoodsId && g.State == 0);
                goods.Stock -= goodsInfo.Number;
                Order order = new Order
                {
                    GoodsId = goods.GoodsId,
                    OrderId = Guid.NewGuid().ToString() + client.ClientId,
                    GoodsName = goods.GoodsName,
                    ClientId = client.ClientId,
                    Consignee = confirmOrderModel.Consignee,
                    Count = goodsInfo.Number,
                    DeliveryAddress = confirmOrderModel.DetailedAddress,
                    Price = goods.Price,
                    PhoneNumber = confirmOrderModel.PhoneNumber,
                    State = -1,
                    CreateTime = DateTime.Now,
                    IsDelete = false
                };
                data.Orders.Add(order);
            }
            foreach (var cart in carts)
            {
                cart.GoodsInfoes.Clear();
                data.ShoppingCarts.Remove(cart);
            }
            data.SaveChanges();
        }
        /// <summary>
        /// 获取未付款订单
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<Order> GetUserUnpaidOrders(int userId)
        {
            var client = data.Clients.SingleOrDefault(c => c.UserId == userId);
            return client.Orders.Where(o => o.State == -1).ToList();
        }
       /// <summary>
       /// 支付订单
       /// </summary>
       /// <param name="userId"></param>
       /// <returns></returns>
        public string PaymentOrders(int userId)
        {
            var client = data.Clients.SingleOrDefault(c => c.UserId == userId);
            var orders = data.Orders.Where(o => o.ClientId == client.ClientId && o.State == -1);
            foreach (var order in orders)
            {
                order.State = 0;
            }
            data.SaveChanges();
            return "支付成功";
        }
        /// <summary>
        /// 获取与用户的所有订单
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public List<Order> GetUserOrders(int userId, int state)
        {
            var client = data.Clients.SingleOrDefault(c => c.UserId == userId);
            switch (state)
            {
                //获取所有订单
                case 10:
                    return client.Orders.Where(o => o.IsDelete == false).ToList();
                //未付款
                case -1:
                    return client.Orders.Where(o => o.IsDelete == false && o.State == -1).ToList();
                //待发货
                case 1:
                    return client.Orders.Where(o => o.IsDelete == false && (o.State == 1 || o.State == 0)).ToList();
                //待确认发货
                case 2:
                    return client.Orders.Where(o => o.IsDelete == false && o.State == 2).ToList();
                //退货中
                case 3:
                    return client.Orders.Where(o => o.IsDelete == false && o.State == 3).ToList();
                //退款中
                case 6:
                    return client.Orders.Where(o => o.IsDelete == false && o.State == 6).ToList();
                    //已完成
                default:
                    return client.Orders.Where(o => o.IsDelete == false &&(o.State == 4 || o.State == 7 || o.State == 9|| o.State == -2)).ToList();

            }
        }
        /// <summary>
        /// 取消订单
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="orderId"></param>
        public void CancelOrder(int userId, string orderId)
        {
            var client = data.Clients.SingleOrDefault(c => c.UserId == userId);
            var order = data.Orders.SingleOrDefault(o => o.OrderId == orderId && o.ClientId == client.ClientId);
            order.State = -2;
            //取消订单后将库存添加回商品
            var goods = data.GoodsInfoes.SingleOrDefault(g => g.GoodsId == order.GoodsId);
            goods.Stock += order.Count;
            data.SaveChanges();
        }
       /// <summary>
       /// 申请退款
       /// </summary>
       /// <param name="userId"></param>
       /// <param name="orderId"></param>
       /// <param name="reasonForApplication"></param>
        public void ApplyForRefund(int userId, string orderId, string reasonForApplication)
        {
            var client = data.Clients.SingleOrDefault(c => c.UserId == userId);
            var order = data.Orders.SingleOrDefault(o => o.OrderId == orderId && o.ClientId == client.ClientId);
            order.ReasonForApplication = reasonForApplication;
            order.ApplicationTime = DateTime.Now;
            order.State = 6;
            data.SaveChanges();
        }
       /// <summary>
       /// 取消退款
       /// </summary>
       /// <param name="userId"></param>
       /// <param name="orderId"></param>
        public void CancelRefund(int userId, string orderId)
        {
            var client = data.Clients.SingleOrDefault(c => c.UserId == userId);
            var order = data.Orders.SingleOrDefault(o => o.OrderId == orderId && o.ClientId == client.ClientId);
            order.ApplicationTime = null;
            order.ReasonForApplication = null;
            order.State = 0;
            data.SaveChanges();
        }
        /// <summary>
        /// 确认收货
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="orderId"></param>
        public void ConfirmReceipt(int userId, string orderId)
        {
            var client = data.Clients.SingleOrDefault(c => c.UserId == userId);
            var order = data.Orders.SingleOrDefault(o => o.OrderId == orderId && o.ClientId == client.ClientId);
            order.State = 9;
            data.SaveChanges();
        }
        /// <summary>
        /// 申请退货
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="orderId"></param>
        /// <param name="reasonForApplication"></param>
        public void ApplicationReturns(int userId, string orderId, string reasonForApplication)
        {
            var client = data.Clients.SingleOrDefault(c => c.UserId == userId);
            var order = data.Orders.SingleOrDefault(o => o.OrderId == orderId && o.ClientId == client.ClientId);
            order.ReasonForApplication = reasonForApplication;
            order.ApplicationTime = DateTime.Now;
            order.State = 3;
            data.SaveChanges();
        }
        /// <summary>
        /// 取消退货
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="orderId"></param>
        public void CancelReturns(int userId, string orderId)
        {
            var client = data.Clients.SingleOrDefault(c => c.UserId == userId);
            var order = data.Orders.SingleOrDefault(o => o.OrderId == orderId && o.ClientId == client.ClientId);
            order.ApplicationTime = null;
            order.ReasonForApplication = null;
            order.State = 2;
            data.SaveChanges();
        }
        /// <summary>
        /// 删除订单记录
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="orderId"></param>
        public void DeleteOrder(int userId, string orderId)
        {
            var client = data.Clients.SingleOrDefault(c => c.UserId == userId);
            var order = data.Orders.SingleOrDefault(o => o.OrderId == orderId && o.ClientId == client.ClientId);
            order.IsDelete = true;
            data.SaveChanges();
        }

        public void Dispose()
        {
            data.Dispose();
        }
    }
}
